package com.hr;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.Index;
import org.hibernate.annotations.Table;

import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.manager.Department;
import com.espirit.eap.manager.SystemLogAware;
import com.espirit.eap.sa.DepartmentAware;
import com.espirit.finance.core.AcctTerm;
import com.espirit.finance.core.AcctTermCheckAware;
import com.googlecode.cswish.model.Saas;


@Entity
@DomainModel
@Table(indexes={
		@Index(name="idx_deptmonthweek", columnNames={"department_id","acctterm_id","week"} )},appliesTo = "hr_schedual")
public class Schedual implements Serializable, DepartmentAware, AcctTermCheckAware, SystemLogAware, Saas{
	private static final long serialVersionUID = -1206858479522520996L;

	public static final String WEEK_PREFIX = "w";
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	private Integer id;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

	@OneToMany(targetEntity = SchedualRecord.class ,orphanRemoval = true, fetch=FetchType.LAZY,  cascade = CascadeType.ALL, mappedBy = "parent")
	public List<SchedualRecord> getChildren() {
		return children;
	}
	public void setChildren(List<SchedualRecord> children) {
		this.children = children;
	}


	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}


//	public String getMonth() {
//		return month;
//	}
//	public void setMonth(String month) {
//		this.month = month;
//	}
	
	public Integer getWeek() {
		return week;
	}
	public void setWeek(Integer week) {
		this.week = week;
	}

	@Transient
	public List<SchedualItem> getSchedualItems() {
		return schedualItems;
	}
	public void setSchedualItems(List<SchedualItem> days) {
		this.schedualItems = days;
	}
	
	@Transient
	public String getDeptIds() {
		return deptIds;
	}
	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}

	@Override
	public boolean check(AcctTerm acctTerm) {
		if (acctTerm != null && acctTerm.getBeginDate() != null && this.acctTerm != null){
			return acctTerm.getName().compareTo(this.acctTerm.getName()) <= 0;
		} else {
			return true;
		}
	}
	
	private Date created;
	private Department department;
	private List<SchedualRecord>  children ;
//	private String month;
	private Integer week;
	private List<SchedualItem> schedualItems;
	private String deptIds;
	private AcctTerm acctTerm;

	@Override
	public String logSource() {
		return acctTerm.getName();
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public AcctTerm getAcctTerm() {
		return acctTerm;
	}
	public void setAcctTerm(AcctTerm acctTerm) {
		this.acctTerm = acctTerm;
	}
}
